<template>
  <div class="app-container">
<!--    {{id}}-->
    <el-form ref="form" :model="intro" label-width="80px">
      <el-form-item label="小说类别">
        <el-input v-model="intro.category"  style="width: 217px;" ></el-input>
      </el-form-item>

<!--      <select v-model="state" placeholder="用户性别">-->
<!--        <option value="1">男</option>-->
<!--        <option value="2">女</option>-->
<!--      </select>-->



      <el-form-item>
<!--        给修改前加上一个判断条件 是否需要修改-->
        <el-button type="primary" @click="onSubmit">修改</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<style>
  /*#app{*/
  /*  text-align: left;*/
  /*}*/
  .el-form{
    float: left;
  }
</style>

<script>
import axios from "axios";

export default {
  name: "roleEditView",
  data(){       //data:{}
    return {
      intro:{
        id:'',
        category:'',
      },


    }
  },
  //获取数据
  created(){
    //通过this.$route.query.id获取当前路由参数中的id值
    this.intro.id=parseInt(this.$route.query.id)
    axios({
      url: '/api/classification/queryById/'+this.intro.id,
      method: 'post',
      headers:{"Authorization":"Bearer "+this.$store.state.user.token}
    }).then(res => {
      console.log(res.data.data)
      // this.user.username=res.data.data[0].username
      // this.user.money=res.data.data[0].money
      // this.user.id=res.data.data[0].id
      this.intro=res.data.data[0]
    })


  },

  methods:{
    //修改数据
    onSubmit(){
      //创建表单对象
      // var formData = new FormData();

      //创建表格对象
      var formData = new window.FormData()
      for (const arr in this.intro) {
        formData.append(arr, this.intro[arr])
      }
      //将fileList中的文件添加到formData中 循环添加文件对象中的raw 后台接收的二进制
      // for (const key in this.fileList) {
      //   formData.append("files",this.fileList[key].raw)
      // }
      this.$confirm('是否确定修改?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        axios({
          url:"/api/classification/update",
          method:"post",
          data:formData,
          //添加请求头
          // headers:{"Content-Type":"multipart/form-data"}
          headers:{"Authorization":"Bearer "+this.$store.state.user.token}
        }).then(res=>{
          if(res.data.code==1){
            this.$message({
              message: '修改成功',
              type: 'success'
            });
            //返回分类表
            this.$router.push("/novel")
          }else {
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });


    },
    cancel(){
      this.$router.push("/novel")
    },



  }

}
</script>

<style scoped>

</style>
